﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Register
{
    public partial class RegisterForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            //Create connection string
            string conn = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();

            //Create command
            SqlCommand cmd1 = new SqlCommand();

            //Create connection
            SqlConnection con = new SqlConnection(conn);

            //Set command text
            cmd1.CommandText = "SELECT [UserName] FROM [CocShopUserInfor].[dbo].[UserInfor]";
            cmd1.Connection = con;  //Set command connection

            //Create data adapter
            SqlDataAdapter da = new SqlDataAdapter(cmd1);

            //Create data table
            DataTable tb = new DataTable();

            //Fill the table
            da.Fill(tb);
            bool flag = true;

            //Check if username is used or not
            foreach (DataRow row in tb.Rows)
            {
                if (((row[0].ToString()).CompareTo(txtusername.Text) == 0))
                {
                    flag = false;
                    break;
                }
            }
            if (!flag)
            {
                lbshow.Text = "This username is being used .Please re-enter !!!";
            }
            else
            {

                try
                {
                    #region Insert User info to database
                    SqlConnection connection = null;
                    string sql = "insert into [CocShopUserInfor].[dbo].[UserInfor]" +
                                 " ([FirstName],[LastName],[Phone],[Email],[UserName],[PassWord]) Values " +
                                 " (@FirstName,@LastName,@Phone,@Email,@UserName,@PassWord)";
                    string url1 = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();

                    try
                    {
                        connection = new SqlConnection(url1);
                        connection.Open();
                        SqlCommand cmd = new SqlCommand(sql, connection);


                        SqlParameter para = new SqlParameter("@FirstName", System.Data.SqlDbType.NChar);
                        para.Value = txtfirstname.Text;
                        cmd.Parameters.Add(para);


                        para = new SqlParameter("@LastName", System.Data.SqlDbType.NChar);
                        para.Value = txtlastname.Text;
                        cmd.Parameters.Add(para);

                        para = new SqlParameter("@Phone", System.Data.SqlDbType.NChar);
                        para.Value = txtphone.Text;
                        cmd.Parameters.Add(para);
                        para = new SqlParameter("@Email", System.Data.SqlDbType.NChar);
                        para.Value = txtemail.Text;
                        cmd.Parameters.Add(para);
                        para = new SqlParameter("@UserName", System.Data.SqlDbType.NChar);
                        para.Value = txtusername.Text;
                        cmd.Parameters.Add(para);
                        para = new SqlParameter("@PassWord", System.Data.SqlDbType.NChar);
                        para.Value = txtpassword.Text;
                        cmd.Parameters.Add(para);
                        cmd.ExecuteNonQuery(); //Insert account info into User database

                        lbshow.Text = "A record is inserted into database";

                        txtlastname.Text = "";
                        txtlastname.Text = "";
                        txtphone.Text = "";
                        txtemail.Text = "";
                        txtusername.Text = "";
                        txtpassword.Text = "";

                        Response.Redirect("Confirmation.aspx");


                    }
                    catch (Exception f)
                    {
                        lbshow.Text = "Error2: " + f.Message;
                    }
                    finally
                    {
                        try
                        {
                            connection.Close();
                        }
                        catch
                        {
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    lbshow.Text = "Error1: " + ex.Message;
                }
                    


            }


        }
    }
}